package com.qianduo.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.qianduo.dao.IQianduoAgencyDao;
import com.qianduo.exception.DaoException;
import com.qianduo.po.QianduoAgency;
import com.qianduo.util.HibernateUtil;
import common.page.PageList;
import common.page.PaginationQuery;
import common.util.StringUtil;

@SuppressWarnings({ "unchecked", "finally" })
public class QianduoAgencyDaoImpl extends HibernateUtil implements IQianduoAgencyDao{
	public void saveOrUpdateAgency(QianduoAgency po) throws DaoException{
		try{
			super.saveOrUpdate(po);
		}catch(Exception e){
			throw new DaoException(className+" saveOrUpdateAgency",e);
		}finally{
			
		}
	}
	
	public List<QianduoAgency> listAgency(Integer id,String username,Integer type,String name,String phone,Integer isCert,Integer isBusinessLicense,Integer dutyCert,Integer cityID,String companyName,String productType,Integer times,String createDateStart,String createDateEnd,PageList pageList) throws DaoException{
		List<QianduoAgency> retList	= null;
		try{
			Criteria ca	= getSessionFactory().getCurrentSession().createCriteria(QianduoAgency.class);
			if(null != id){
				ca.add(Restrictions.eq("id", id));
			}
			if(!StringUtil.isEmpty(username)){
				ca.add(Restrictions.like("username", "%"+username+"%"));
			}
			if(null != type){
				ca.add(Restrictions.eq("type", type));
			}
			if(!StringUtil.isEmpty(name)){
				ca.add(Restrictions.like("name", "%"+name+"%"));
			}
			if(!StringUtil.isEmpty(phone)){
				ca.add(Restrictions.like("phone", "%"+phone+"%"));
			}
			if(null != isCert){
				ca.add(Restrictions.eq("isCert", isCert));
			}
			if(null != isBusinessLicense){
				ca.add(Restrictions.eq("isBusinessLicense", isBusinessLicense));
			}
			if(null != dutyCert){
				ca.add(Restrictions.eq("dutyCert", dutyCert));
			}
			if(null != cityID){
				ca.add(Restrictions.eq("cityID", cityID));
			}
			if(!StringUtil.isEmpty(companyName)){
				ca.add(Restrictions.like("companyName", "%"+companyName+"%"));
			}
			if(!StringUtil.isEmpty(productType)){
				ca.add(Restrictions.like("products", "%|"+productType+"|%"));
			}
			if(null != times){
				ca.add(Restrictions.ge("times", times));
			}
			if(!StringUtil.isEmpty(createDateStart)){
				ca.add(Restrictions.ge("createDate",createDateStart));
			}
			if(!StringUtil.isEmpty(createDateEnd)){
				ca.add(Restrictions.le("createDate",createDateEnd));
			}
			if(null != pageList){				
				retList	= PaginationQuery.queryCriteria(ca, pageList);
			}else{
				retList	= ca.list();
			}
		}catch(Exception e){
			throw new DaoException(className+" listAgency",e);
		}finally{
			return retList;
		}
	}
	
	public QianduoAgency getAgencyByUser(String username) throws DaoException{
		QianduoAgency ret	= null;
		try{
			Criteria ca	= getSessionFactory().getCurrentSession().createCriteria(QianduoAgency.class);
			ca.add(Restrictions.eq("username",username));
			Object obj	= ca.uniqueResult();
			if(null != obj){
				ret		= (QianduoAgency)obj;
			}
		}catch(Exception e){
			throw new DaoException(className+" getAgencyByUser",e);
		}finally{
			return ret;
		}
	}
}
